package com.wica.view;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;

import com.wica.facetube.FaceTubeService;
import com.wica.facetube.R;
import com.wica.media.Player;

import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.os.SystemClock;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;
import android.widget.Toast;

public class Rating extends Activity implements RatingBar.OnRatingBarChangeListener {
	private static final String LOG_TAG = "Rating";	
	private static final String[] RATINGS =  new String[] {"Poor","Poor","Nothing Special","Worth Watching","Pretty Cool","Awesome!"};

	protected FaceTubeService fts;
	

	private RatingBar ratingSelector;
	private URL url;
	private InputStream is;
	private BitmapDrawable bmp;
	private ImageView image;	
	
	int percentageWatched = 100;
	String locationTxt = "At home";
	String location = "";
	String environment ="Alone";
	String URL="http://www.youtube.com/v/dMH0bHeiRNg";
	double totalrating = 0.00;
	private RatingBar totalRatingBar;
	private TextView totalRatingText,titleTextView,mRatingText;

	private String title;
	private Integer rating=0;
	private String mood;
	private float latitude;
	private float longitude;
	private float altitude;
//	private Button skipRating;
	protected Integer elrating;
	protected long eluserid;
		
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        Log.v(LOG_TAG,"oncreaterating");
        // We inflate layout from rating.xml
        setContentView(R.layout.rating);   
        
        totalrating=new Double(this.getIntent().getExtras().get("totalRating").toString());
        title = this.getIntent().getExtras().get("title").toString();        
        titleTextView = (TextView) findViewById(R.id.title);
        rating = Integer.valueOf(this.getIntent().getExtras().get("rating").toString());
        titleTextView.setText(title);
        mRatingText = (TextView) findViewById(R.id.rating);
        if (rating != 0)
        	mRatingText.setText(RATINGS[rating]);
        totalRatingText = (TextView) findViewById(R.id.totalrating);
        totalRatingText.setText("Users Rating: "+totalrating+"/5");

        // We copy the most recently changed rating on to these indicator-only
        // rating bars

        // The different rating bars in the layout. Assign the listener to us.
        ratingSelector=(RatingBar)findViewById(R.id.ratingbar2);
        totalRatingBar=(RatingBar)findViewById(R.id.small_totalratingbar);
        totalRatingBar.setRating((float)totalrating);
        ratingSelector.setRating(Float.valueOf(rating.toString()));
//        ratingSelector.setOnTouchListener(this);
//        ratingSelector.set
        (ratingSelector).setOnRatingBarChangeListener(this);
//        skipRating = (Button) findViewById(R.id.btn_skiprating);
//        skipRating.setOnClickListener(
//                new OnClickListener() {
//                    public void onClick(View v) {                    	
//                    	finish();
//                    }
//                }
//            ); 
//        skipRating.setEnabled(false);
    	URL=this.getIntent().getExtras().get("URL").toString();
    	
        try {
			url = new URL(this.getIntent().getExtras().get("URLThumbnail").toString());
	        is = (InputStream) url.getContent();
		} catch (MalformedURLException e) {
			Log.e(LOG_TAG,e.toString());
		} catch (IOException e) {
			Log.e(LOG_TAG,e.toString());
		}
        bmp = (BitmapDrawable) BitmapDrawable.createFromStream(is , "youtube");
        image = (ImageView) findViewById(R.id.imageview);
        image.setImageDrawable(bmp);
        
       	Intent intent = new Intent(Rating.this, Player.class);
        intent.putExtra("rating", rating);
    	setResult(RESULT_OK,intent);
        
        fts = new FaceTubeService();   
        Log.v(LOG_TAG,"finoncreaterating");
    }//onCreate method

	protected void rate(int rating) {
    	//session, percentage, location, mood, environment, url
		mood=PreferenceManager.getDefaultSharedPreferences(this).getString("mood", "Happy");
		if (mood.compareTo("")==0)
			mood="Happy";
		latitude=PreferenceManager.getDefaultSharedPreferences(this).getFloat("latitude", 0);
		longitude=PreferenceManager.getDefaultSharedPreferences(this).getFloat("longitude", 0);
		altitude=PreferenceManager.getDefaultSharedPreferences(this).getFloat("altitude", 0);
		elrating=rating;
		eluserid=new Long(PreferenceManager.getDefaultSharedPreferences(this).getInt("userID", -1));
		Thread thread = new Thread(){
			@Override
			public void run() {
				// TODO Auto-generated method stub
		       	fts.faceTubeAddWatchedItemAdrian(eluserid,percentageWatched,elrating, locationTxt, mood, environment, URL,location);
			}
		};
		thread.start();
       	Intent intent = new Intent(Rating.this, Player.class);
        intent.putExtra("rating", rating);
    	setResult(RESULT_OK,intent);
    	finish();    	
        Toast.makeText(this, "Video was rated with "+rating+" star"+(rating>1?"s":""), Toast.LENGTH_SHORT).show();
	}//rate method	

	public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromTouch) {
        mRatingText.setText(RATINGS[new Integer((new Float(rating)).intValue())]);
        if (rating<1){
        	ratingBar.setRating(1);
        	rating=1;
        }
        int intRating = new Float(rating).intValue();
        rate(intRating);
    }
	
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode==KeyEvent.KEYCODE_BACK){
	        Toast.makeText(this, "Please rate this video", Toast.LENGTH_LONG).show();
	        return true;
		}
		return super.onKeyDown(keyCode, event);
	}
	
}//Rating class